using APITransLib.Contracts;
using ChatWithMulitAIServer.Data;
using Microsoft.AspNetCore.Mvc;

namespace ChatWithMulitAIServer.Controllers;

[ApiController]
[Route("api/groups")]
public class GroupsController(IChatRepository repo) : ControllerBase
{
    [HttpPost]
    public async Task<ActionResult<CreateGroupResponse>> Create(CreateGroupRequest req, CancellationToken ct)
    {
        if (string.IsNullOrWhiteSpace(req.Name)) return BadRequest();
        var group = await repo.CreateGroupAsync(req.Name, req.MemberUserIds, ct);
        return Ok(new CreateGroupResponse { Group = group });
    }

    [HttpPost("add-users")]
    public async Task<IActionResult> AddUsers(AddUsersToGroupRequest req, CancellationToken ct)
    {
        var g = await repo.GetGroupAsync(req.GroupId, ct);
        if (g is null) return NotFound();
        await repo.AddUsersToGroupAsync(req.GroupId, req.UserIds, ct);
        g = await repo.GetGroupAsync(req.GroupId, ct);
        return Ok(g);
    }
}
